package src.main.java.chapter2;

import java.util.IllformedLocaleException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author lwx
 * @create 2016-09-25 16:27
 */
public class Lesson_1 {

}

//定义接口
interface Service {

    //todo  具体接口定义
}


//接口提供
interface Provider {
    Service newService();

}


//接口注册和访问
class Services {

    private static final Map<String, Provider> providers = new ConcurrentHashMap<String, Provider>();

    public static final String DEFAULT_PROVIDER_NAME = "<def>";

    private Services() {
    }//不允许直接被调用

    public static void registerDefaultProvider(Provider p) {


        registerProvider(DEFAULT_PROVIDER_NAME, p);

    }

    public static void registerProvider(String name, Provider p) {
        providers.put(name, p);

    }

    public static Service newInstance() {

        return newInstance(DEFAULT_PROVIDER_NAME);
    }


    public static Service newInstance(String name) {

        Provider p = providers.get(name);
        if (p == null) {

            throw new IllformedLocaleException("no Provider registered with name:" + name);
        }
        return p.newService();

    }
}

